/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slisfx.devices;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.scene.control.Button;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import slisfx.Globals;
import slisfx.resources.Load;
import slisfx.screens.DeviceViewAdminFX;
import slisfx.server.Client;

/**
 *
 * @author ITC
 */
public class AlarmViewAdminFX extends Application implements Globals  {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(final Stage primaryStage) {
        try {
            StackPane stackPane = new StackPane();
            
            //This is for horizantal layout.
            HBox imageBox = new HBox();
            imageBox.setPadding(new Insets(10, 10, 10, 200));
            imageBox.setSpacing(100);

            Image image = new Image(new Load().loadFile("gray.png"));
            Image image1 = new Image(new Load().loadFile("NASA_IVV1_sm.png"));
            Image image2 = new Image(new Load().loadFile("WVU_LOGO_sm.png"));

            ImageView iv1BackGround = new ImageView();
            iv1BackGround.setImage(image);
            ImageView iv1 = new ImageView();
            iv1.setImage(image1);
            ImageView iv2 = new ImageView();
            iv2.setImage(image2);
            
            imageBox.getChildren().addAll(iv1, iv2);
            
            primaryStage.setTitle("Alarm menu");
            primaryStage.setResizable(false);
            
            Button btn = new Button();
            
            final ToggleGroup alarmStatus = new ToggleGroup();

            final RadioButton rb1 = new RadioButton("Alarm Off");
            rb1.setToggleGroup(alarmStatus);
            rb1.setSelected(true);
            rb1.setTextFill(Color.WHITE);

            final RadioButton rb2 = new RadioButton("Armed home");
            rb2.setToggleGroup(alarmStatus);
            rb2.setTextFill(Color.WHITE);

            final RadioButton rb3 = new RadioButton("Armed away");
            rb3.setToggleGroup(alarmStatus);
            rb3.setTextFill(Color.WHITE);
            
//            switch(x10.getAlarmState())
//            {
//                case 0: alarmStatus.selectToggle(rb1);
//                        break;
//                case 1: alarmStatus.selectToggle(rb2);
//                        break;
//                case 2: alarmStatus.selectToggle(rb3);
//                        break;
//            }
            
           
    alarmStatus.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
        @Override
        public void changed(ObservableValue<? extends Toggle> ov,
            Toggle old_toggle, Toggle new_toggle) {
                if(rb1.isSelected()){
                    System.out.println("Stand down!");
                    //x10.setAlarmState(0);
                    new Client().connectServer(ipAddress,port, "E1"); 
                }
                else if(rb2.isSelected()){
                    System.out.println("I'll give you a warning when the bad guys come");
                    //x10.setAlarmState(1);
                    new Client().connectServer(ipAddress,port, "E8");
                }
                else if(rb3.isSelected()){
                    System.out.println("No one is home, arm all systems!");
                    //x10.setAlarmState(2);
                    new Client().connectServer(ipAddress,port, "E9");
                }
                
        }});
    btn.setText("Return");           
    btn.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                    new DeviceViewAdminFX().start(primaryStage);
                }
            });
            
            BorderPane mainWindow = new BorderPane();
            
            /* Here we can add images instead of Rectangle in the Future*/
            
            mainWindow.setTop(new Rectangle(830,50, Color.BLACK));
            mainWindow.setBottom(new Rectangle(830,50, Color.BLACK));
            mainWindow.setLeft(new Rectangle(50,350, Color.BLACK));
            mainWindow.setRight(new Rectangle(50, 350, Color.BLACK));
            
            
            HBox alarm = new HBox();
            alarm.setPadding(new Insets(10, 10, 10, 180));
            alarm.setSpacing(20);
            alarm.getChildren().add(rb1);
            alarm.getChildren().add(rb2);
            alarm.getChildren().add(rb3);
            alarm.getChildren().add(btn);
    //        
    //        HBox onOffBut = new HBox();
    //        onOffBut.setPadding(new Insets(10, 10, 10, 10));
    //        onOffBut.setSpacing(50);
    //        onOffBut.getChildren().add(btn);
    //        onOffBut.getChildren().add(btn2);
            
            
            VBox mainMenu = new VBox();
            mainMenu.setPadding(new Insets(10, 10, 10, 10));
            mainMenu.setSpacing(40);
            Text title = new Text("Alarm menu");
            title.setFont(Font.font("Amble CN", FontWeight.BOLD, 14));
            title.setFill(Color.WHITE);
            mainMenu.getChildren().addAll(title, imageBox, alarm);
    //        mainMenu.getChildren().add(slider);
    //        mainMenu.getChildren().add(onOffBut);
    //        
            stackPane.getChildren().addAll(iv1BackGround, mainMenu);
            mainWindow.setCenter(stackPane);

            primaryStage.setScene(new Scene(mainWindow, WIDTH, HEIGHT));
            primaryStage.show();
        } catch (IOException ex) {
            Logger.getLogger(AlarmViewAdminFX.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
